package com.changgou.search.controller;

import com.changgou.search.service.SkuService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 秦川熙
 */
@RestController
@RequestMapping(value = "/search")
@CrossOrigin
public class SkuController {

    @Autowired
    private SkuService skuService;

    /**
     * 导入数据
     * @return
     */
    @GetMapping("/import")
    public Result importData() {
        skuService.importEs();
        return new Result(true, StatusCode.OK, "导入数据到索引库中成功");
    }

    /**
     * 搜索商品数据
     * @param searchMap
     * @return
     */
    @GetMapping
    public Map search(@RequestParam(required = false) Map searchMap) {
        Object pageNum = searchMap.get("pageNum");
        if (pageNum == null) {
            searchMap.put("pageNum", "1");
        }

        if (pageNum instanceof Integer) {
            searchMap.put("pageNum", pageNum.toString());
        }
        return skuService.search(searchMap);
    }
}
